//
//  OADepAddressController.m
//  OASystem
//
//  Created by zhoupengfei on 16/1/17.
//  Copyright © 2016年 zpf. All rights reserved.
//
#import "nameTableViewCell.h"

#import "OADepAddressController.h"
#import "HttpTools.h"
#import "MBProgressHUD.h"
#import "OAAddressDetailViewController.h"
#import "Toast+UIView.h"









@interface OADepAddressController ()

@end

@implementation OADepAddressController

-(NSMutableArray*)contents{
    if (_contents == nil) {
        _contents = [NSMutableArray array];
    }
    return _contents;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"通讯录";

    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"登录页bg"]];
 self.navigationController.navigationBar.barTintColor = color; self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
  NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName, nil];
[self.navigationController.navigationBar setTitleTextAttributes:attributes];
 self.view.backgroundColor = [UIColor whiteColor];
   _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];

          [self requestData];
  
    [self setRightItem];
}

-(void)setRightItem{
    UIBarButtonItem * rightitem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
    self.navigationItem.rightBarButtonItem = rightitem;
}


-(void)searchAction{
    
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"搜索通讯录" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertView.delegate = self;
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * textField = [alertView textFieldAtIndex:0];
    textField.placeholder = @"请输入关键字";
    [alertView show];
}
//搜素通讯录
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        UITextField * textField = [alertView textFieldAtIndex:0];
        if (textField.text.length == 0) {
            return;
        }
        [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        NSArray *  array = @[@{@"condition":textField.text},@{@"userId":userId},@{@"page":@"1"}];;
        [HttpTools requestWithURL:KExchother arrays:array method:@"adressListSearch" success:^(id JSON) {
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
            NSArray * tempArray = JSON[@"data"];
            NSDictionary *dict = [tempArray firstObject];
            
            if (dict) {
//                NSArray * array = @[dict];
                self.contents = [tempArray mutableCopy];
            }
            if (self.contents.count == 0) {
                [self.view makeToast:@"搜索无相关数据"];
            }
            
            [self.tableView reloadData];
            
            
        } failure:^(NSError *error) {
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        }];
    }
}


-(void)requestData{
      [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    _contents = [NSMutableArray array];
   // NSArray *  array =[NSArray arrayWithObjects:@{@"path":self.path}, nil];
        self.path = @"hzcj";
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];

//      NSArray *  array = @[@{@"path":self.path}];
        NSArray * array = @[@{@"userId":userId}];

    
    
      [HttpTools requestWithURL:KExchother arrays:array method:@"globalTxlTree" success:^(id JSON) {
      NSLog(@"%@",JSON);
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        NSArray * tempArray = JSON[@"data"];
        NSDictionary *dict = [tempArray firstObject];
        self.contents = dict[@"Parent"];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
     NSDictionary *dict = self.contents[indexPath.row];

 static NSString * cellIdentifier = @"cellId";

    nameTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

            if (cell == nil) {
                cell = [[nameTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
                                                           }


         if ([dict[@"phone"]isEqualToString:@""]) {
    cell.phone = @"no";
}

    cell.dict = dict;
//}
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contents.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 return 44;
 }

-(UIColor *) randomColor
{
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

@end
